package student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class StudentsClass {

    String name;
    String address;
    String phone;
    int flag=0;

    public void setName( String value )
    {
        name = value;
    }

    public void setAddress( String value )
    {
        address = value;
    }

    public void setPhone( String value )
    {
        phone = value;
    }

    public String getName() { return name; }

    public String getAddress() { return address; }

    public String getPhone() { return phone; }
    
    public void addItem()
    {
	   	flag=0;
	    Connection con = null;
	    try {
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch (Exception e)
		{ System.out.print("Error loading driver."); }
		try {
		con = DriverManager.getConnection("jdbc:odbc:jdbctest", "admin", "admin");
		}
		catch (Exception e)
		{ System.out.print("Error connecting to DataSource."); }
		try {
	    Statement stmt = con.createStatement();
	    String strQuery ="insert into student values('" + name + "', '" + address + "', '" + phone + "')";
 		stmt.execute(strQuery);
		}
		catch (Exception e) {
			System.out.print("Error with SQL.");
		}
    }
    
    public void setFlag(int val)
    {
	    flag=val;
    }
    public int getFlag() { return flag; }
    
    public void update(String update)
    {
	    flag=0;
	    Connection con = null;
	    try {
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch (Exception e)
		{ System.out.print("Error loading driver."); }
		try {
		con = DriverManager.getConnection("jdbc:odbc:jdbctest", "admin", "admin");
		}
		catch (Exception e)
		{ System.out.print("Error connecting to DataSource."); }
	    try {
		Statement stmt = con.createStatement();
	    String strQuery ="UPDATE student SET  name='" + name + "', address='" + address + "', phone='" + phone + "' where name='" + update +"'";
 		stmt.execute(strQuery);	
	    }
	    catch (Exception e) {
			System.out.print("Error with SQL.");
		}
    }
    
    public void delete(String delete)
    {
	    Connection con = null;
	    try {
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch (Exception e)
		{ System.out.print("Error loading driver."); }
		try {
		con = DriverManager.getConnection("jdbc:odbc:jdbctest", "admin", "admin");
		}
		catch (Exception e)
		{ System.out.print("Error connecting to DataSource."); }
	    try {
		Statement stmt = con.createStatement();
	    String strQuery ="delete from student where name='" + delete + "'";
	    stmt.execute(strQuery);
	    }
	    catch (Exception e) {
			System.out.print("Error with SQL.");
		}
    }
}